package com.mozz.finally_work.servlet.api;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mozz.finally_work.dao.CategoryDao;
import com.mozz.finally_work.pojo.Category;
import com.mozz.finally_work.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;
import java.util.Set;

@WebServlet(name = "QueryAllCategoryServlet", value = "/QueryAllCategoryServlet")
public class QueryAllCategoryServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*获取参数*/
        String userId = request.getParameter("userId");
        /*查询数据库*/
        SqlSession session = MybatisUtils.getSession();
        CategoryDao dao = session.getMapper(CategoryDao.class);
        List<Category> categories = dao.queryALlCategoryByUserId(userId);
        session.close();
        ObjectMapper mapper = new ObjectMapper();
        String ret = mapper.writeValueAsString(categories);
        response.setHeader("Content-Type", "application/json; charset=UTF-8");
        response.getWriter().write(ret);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
